package tech.jabari.user.dao.impl;

import tech.jabari.user.dao.UserDao;
import tech.jabari.user.entity.User;
import tech.jabari.utils.JdbcUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Jabari Lu
 * @title: UserDaoImpl
 * @data 2020/8/4 14:25
 */
public class UserDaoImpl implements UserDao {

    @Override
    public List<User> findAllUser(Map<String,Object> paramsMap) {
        Integer pageSize = (Integer)paramsMap.get("pageSize");
        Integer beginNo = (Integer)paramsMap.get("beginNo");
        String username = (String)paramsMap.get("username");
        List<User> userList = new ArrayList<>();
        List<Map<String, Object>> userDataList = JdbcUtil.query(
                "select * from t_user where username like ? order by id desc limit ?,?"
                ,"%"+username+"%",beginNo,pageSize);
        if (null ==  userDataList || userDataList.size() == 0) {
            return null;
        }
        for (Map<String, Object> data : userDataList) {
            User user = new User();
            user.setId((Integer)data.get("id"));
            user.setUsername((String)data.get("username"));
            user.setPwd((String)data.get("pwd"));
            user.setEmail((String)data.get("email"));
            user.setPhone((String)data.get("phone"));
            user.setSex((String)data.get("sex"));
            user.setCreateTime((Date)data.get("create_time"));
            user.setBirth((Date)data.get("birth"));
            userList.add(user);
        }
        return userList;
    }


    @Override
    public Integer findAllUserCount(Map<String, Object> paramsMap) {
        String username = (String)paramsMap.get("username");
        String sqlCount = "select count(*) as cnt from t_user where username like ?";
        List<Map<String,Object>> list = JdbcUtil.query(sqlCount, "%" + username + "%");
        Long cnt = (Long)list.get(0).get("cnt");
        return cnt.intValue();
    }


    @Override
    public Integer deleteUser(Integer id) {
        String sql = "delete from t_user where id = ?";
        int r = JdbcUtil.update(sql, id);
        return r;
    }


    /**
     * 根据id数组批量删除用户
     * @param delIds - String - 格式： 1,2,3
     * @return
     */
    @Override
    public Integer batchDeleteUser(String delIds) {
        String sql = "delete from t_user where id in ("+delIds+")";
        int r = JdbcUtil.update(sql, null);
        return r;
    }


    @Override
    public User findById(Integer idInt) {
        String sql ="select * from t_user where id = ?";
        List userList = JdbcUtil.query(sql, idInt);
        User user = null;
        if (null != userList && userList.size() > 0) {
            Map<String,Object> data = (Map<String, Object>) userList.get(0);
            user = new User();
            user.setId((Integer)data.get("id"));
            user.setUsername((String)data.get("username"));
            user.setPwd((String)data.get("pwd"));
            user.setEmail((String)data.get("email"));
            user.setPhone((String)data.get("phone"));
            user.setSex((String)data.get("sex"));
            user.setCreateTime((Date)data.get("create_time"));
            user.setBirth((Date)data.get("birth"));
        }
        return user;
    }
}
